package com.xuelang.pipeline.app.resource.impl;

import com.xuelang.hiforce.base.tracing.TracingUtils;
import com.xuelang.pipeline.app.resource.DictService;
import com.xuelang.pipeline.app.resource.converter.DictDTOToDictConverter;
import com.xuelang.pipeline.client.common.request.PageParam;
import com.xuelang.pipeline.client.common.result.PageRecord;
import com.xuelang.pipeline.client.common.util.BizAssert;
import com.xuelang.pipeline.client.resource.dto.DictDTO;
import com.xuelang.pipeline.client.resource.dto.DictQuery;
import com.xuelang.pipeline.domain.resource.Dict;
import com.xuelang.pipeline.domain.resource.service.DictDomainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 字典服务实现类
 *
 * @author xiwen.zxg
 * @since 2024/5/15
 */
@Service
public class DictServiceImpl implements DictService {

    @Autowired
    DictDomainService dictDomainService;

    @Override
    public PageRecord<DictDTO> pageList(PageParam<DictQuery> dtoPageParam) {
        DictQuery condition = dtoPageParam.getCondition();
        condition = condition == null ? new DictQuery() : condition;
        PageRecord<Dict> pageRecord = dictDomainService.pageList(
                dtoPageParam.getCurrentPage(),
                dtoPageParam.getPageSize(),
                condition);
        return new PageRecord<>(
                pageRecord.getCurrentPage(),
                pageRecord.getPageSize(),
                pageRecord.getTotalCount(),
                pageRecord.getTotalPages(),
                pageRecord.getRecords().stream().map(DictDTOToDictConverter.INSTANCE::dictToDictDTO).collect(Collectors.toList()));
    }

    @Override
    public List<DictDTO> list(DictQuery dictQuery) {
        return dictDomainService.list(dictQuery).stream().map(DictDTOToDictConverter.INSTANCE::dictToDictDTO).collect(Collectors.toList());
    }


    @Override
    public Boolean add(DictDTO dictDTO) {
        BizAssert.notEmpty(dictDTO.getDictType(), "dictType不能为空");
        BizAssert.notEmpty(dictDTO.getDictCode(), "dictCode不能为空");
        BizAssert.notEmpty(dictDTO.getDictName(), "dictName不能为空");
        BizAssert.notEmpty(dictDTO.getDictValue(), "dictValue不能为空");
        BizAssert.notNull(dictDTO.getStatus(), "status不能为空");
        Dict dict = DictDTOToDictConverter.INSTANCE.dictDTOToDict(dictDTO);
        dict.setCreatorId(TracingUtils.getUserId());
        dict.setUpdaterId(TracingUtils.getUserId());
        return dictDomainService.save(dict);
    }

    @Override
    public Boolean update(DictDTO dictDTO) {
        BizAssert.notNull(dictDTO.getId(), "id不能为空");
        BizAssert.notEmpty(dictDTO.getDictType(), "dictType不能为空");
        BizAssert.notEmpty(dictDTO.getDictCode(), "dictCode不能为空");
        BizAssert.notEmpty(dictDTO.getDictName(), "dictName不能为空");
        BizAssert.notEmpty(dictDTO.getDictValue(), "dictValue不能为空");
        BizAssert.notNull(dictDTO.getStatus(), "status不能为空");
        Dict dict = DictDTOToDictConverter.INSTANCE.dictDTOToDict(dictDTO);
        dict.setCreatorId(TracingUtils.getUserId());
        dict.setUpdaterId(TracingUtils.getUserId());
        return dictDomainService.update(dict);
    }

    @Override
    public Boolean delete(Long id) {
        BizAssert.notNull(id, "id不能为空");
        return dictDomainService.delete(id);
    }

    @Override
    public DictDTO query(Long id) {
        BizAssert.notNull(id, "id不能为空");
        return DictDTOToDictConverter.INSTANCE.dictToDictDTO(dictDomainService.query(id));
    }

}
